﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.Threading;
using Azku.Common;

namespace Azku.ClientUtil
{
    public class Client : IDisposable
    {
        private int m_LocalPort = 6666;
        private IPEndPoint m_HostIPEndPoint = null;

        public IPEndPoint HostIPEndPoint
        {
            get { return m_HostIPEndPoint; }
            set { m_HostIPEndPoint = value; }
        }
        private UdpClient m_UdpClient = null;

        private static int m_ThreadNumber = 5;
        private static Thread[] m_ThreadArray = new Thread[m_ThreadNumber];

        private ErrorLog m_ErrorLog;

        public ErrorLog ErrorLog
        {
            get { return m_ErrorLog; }
            set { m_ErrorLog = value; }
        }



        public Client(string strIPAddress, int port)
        {
            m_HostIPEndPoint = new IPEndPoint(IPAddress.Parse(strIPAddress), port);

            m_UdpClient = new UdpClient(m_LocalPort);
        }

        public void ThreadStart()
        {
            for (int i = 0; i < m_ThreadNumber; i++)
            {
                Thread tempThread = new Thread(Listener);
                tempThread.IsBackground = true;
                tempThread.Start();
                m_ThreadArray[i] = tempThread;
            }
        }

        private void Listener()
        {
            while (true)
            {
                try
                {
                    IPEndPoint pIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    Byte[] buffer = m_UdpClient.Receive(ref pIPEndPoint);

                    object pReceiveObj = Common.ZipSerializable.DeserializeObject(buffer);
                    if (pReceiveObj != null)
                    {
                        if (pReceiveObj is STCMessageBase)
                        {
                            STCMessageBase pCTSUserLogin = (STCMessageBase)pReceiveObj;
                            DataManager.Instance.MessageCollection.Insert(pCTSUserLogin);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ErrorLog(ex.Message);
                }
            }
        }

        public void SendMsg(MessageBase msg, IPEndPoint ipEndPoint)
        {
            if (msg == null) { return; }
            byte[] buffer = ZipSerializable.SerializeObject(msg);
            m_UdpClient.Send(buffer, buffer.Length, ipEndPoint);
        }

        #region IDisposable 成员

        public void Dispose()
        {
            for (int i = 0; i < m_ThreadNumber; i++)
            {
                if (m_ThreadArray[i] != null && m_ThreadArray[i].ThreadState != ThreadState.Aborted)
                    m_ThreadArray[i].Abort();
            }
            m_UdpClient.Close();
        }

        #endregion
    }
}
